package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.ApplyRefundReq;
import com.ymatou.iapi.optrade.parameter.req.ReapplyRefundReq;
import com.ymatou.iapi.optrade.parameter.vo.SalesRefundInfoVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ReapplyRefundCall;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.DataUtil;

/**
 * 对已关闭退单重新开启接口 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_ReapplyRefund {

	private static ReapplyRefundReq bean;
	private static ReapplyRefundCall call;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("对已关闭退单重新开启接口");
	}

	@Before
	public void caseUp() {

		bean = new ReapplyRefundReq();
		call = new ReapplyRefundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_001() {
		Logger.start(true, "对已关闭退单重新开启成功");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.placeOrder1(
					false, false);

			Order order = placeOrderResp.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			//拒绝退款
			ApplyRefundReq applyRefundReq = RefundService
					.rejectRefundBill(order);

			String refundBillNo = applyRefundReq.getSalesRefundInfoList()
					.get(0).getRefundBillNo();

			bean.setOrderId(orderId);
			bean.setRefundAmount(BigDecimal.TEN);
			bean.setRefundBillNo(refundBillNo);
			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证code");

			DataUtil.verifyDecimalEquals(
					BigDecimal.ZERO,
					BigDecimal.valueOf((Integer) call.getData().get(
							"refundFreight")), "验证refundFreight");

			// 数据库验证

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setTotalAmount(bean.getRefundAmount());

			expectedRefundBillPo.setProductCount(1);

			expectedRefundBillPo.setFreight(BigDecimal.ZERO);

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);

			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_002() {
		Logger.start(true, "退货退款中退单重新开启失败");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			Order order = placeOrderResp.getOrderList().get(0);

			Long orderId = order.getOrderId();

			bean.setOrderId(orderId);
			bean.setRefundAmount(BigDecimal.TEN);
			bean.setRefundBillNo("TK0" + orderId);
			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("201", call.getString("code"), "验证code");
			Logger.verifyEquals("102", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals(
					String.format("退款单%s状态不正确", bean.getRefundBillNo()),
					call.getString("msg"), "验证msg");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_003() {
		Logger.start(true, "退货退款完成退单重新开启失败");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			Long orderId = order.getOrderId();

			//退款执行
			ApplyRefundReq applyRefundReq = RefundService.approveRefund(order);

			bean.setOrderId(orderId);
			bean.setRefundAmount(BigDecimal.TEN);
			bean.setRefundBillNo(applyRefundReq.getSalesRefundInfoList().get(0)
					.getRefundBillNo());
			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("201", call.getString("code"), "验证code");
			Logger.verifyEquals("102", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals(
					String.format("退款单%s状态不正确", bean.getRefundBillNo()),
					call.getString("msg"), "验证msg");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_004() {
		Logger.start(true, "对已关闭退单重新开启超过最大商品可退金额");
		try {

			TradingServiceResp placeOrderResp = TradingCallService.placeOrder1(
					false, false);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			//拒绝退款
			ApplyRefundReq applyRefundReq = RefundService
					.rejectRefundBill(order);

			SalesRefundInfoVo salesRefundInfoVo = applyRefundReq
					.getSalesRefundInfoList().get(0);

			String refundBillNo = salesRefundInfoVo.getRefundBillNo();

			bean.setOrderId(orderId);
			bean.setRefundAmount(salesRefundInfoVo.getRefundAmount().add(
					BigDecimal.ONE));
			bean.setRefundBillNo(refundBillNo);
			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("201", call.getString("code"), "验证code");

			Logger.verifyEquals("114", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals("申请退款金额不能大于实际可退款金额", call.getString("msg"),
					"验证msg");

			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, new BigDecimal(call
					.getData().getDouble("refundFreight")), "验证refundFreight");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_005() {
		Logger.start(true, "对已关闭退单重新开启成功,校验退的运费");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.placeOrder3(true);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();
			Long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			BigDecimal proFreight = order.getOrderDetailPoList().get(0)
					.getFreight();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			ApplyRefundReq applyRefundReq = RefundService
					.rejectRefundBill(order);

			SalesRefundInfoVo salesRefundInfoVo = applyRefundReq
					.getSalesRefundInfoList().get(0);

			BigDecimal refoundAmout = salesRefundInfoVo.getRefundAmount();

			bean.setOrderId(orderId);
			bean.setRefundAmount(refoundAmout.add(BigDecimal.ONE));
			bean.setRefundBillNo(salesRefundInfoVo.getRefundBillNo());
			bean.setRefundProductNum(salesRefundInfoVo.getRefundProductNum());

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证code");

			Logger.verifyEquals("0", call.getString("bcode"), "验证bcode");

			DataUtil.verifyDecimalEquals(proFreight.add(BigDecimal.ONE),
					new BigDecimal(call.getData().getDouble("refundFreight")),
					"验证refundFreight");

			Logger.verifyEquals(true, call.getData().getBoolean("success"),
					"验证success");

			// 数据库验证

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(salesRefundInfoVo
					.getRefundBillNo());
			expectedRefundBillPo.setCatalogId(placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList().get(0).getCatalogId());
			expectedRefundBillPo.setTotalAmount(salesRefundInfoVo
					.getRefundAmount().add(BigDecimal.ONE));

			expectedRefundBillPo.setProductCount(bean.getRefundProductNum());

			expectedRefundBillPo.setFreight(proFreight.add(BigDecimal.ONE));

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(salesRefundInfoVo
							.getRefundBillNo());

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_006() {
		Logger.start(true, "订单号不存在");
		try {

			bean.setOrderId(11111);
			bean.setRefundAmount(BigDecimal.TEN);
			bean.setRefundBillNo("2");
			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("201", call.getString("code"), "验证code");
			Logger.verifyEquals("110", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals(
					String.format("退款单%s不存在", bean.getRefundBillNo()),
					call.getString("msg"), "验证msg");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_007() {
		Logger.start(true, "退款单不存在");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			bean.setOrderId(orderId);
			bean.setRefundAmount(BigDecimal.TEN);
			bean.setRefundBillNo("234234");
			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("201", call.getString("code"), "验证code");
			Logger.verifyEquals("110", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals(
					String.format("退款单%s不存在", bean.getRefundBillNo()),
					call.getString("msg"), "验证msg");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_008() {
		Logger.start(true, "订单号为null");
		try {
			//bean.setOrderId(11111);
			bean.setRefundAmount(BigDecimal.TEN);
			bean.setRefundBillNo("222");
			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证code");
			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals("orderId:订单号不能为空", call.getString("msg"),
					"验证msg");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReapplyRefund_009() {
		Logger.start(true, "退款单号为null");
		try {

			bean.setOrderId(11111);
			bean.setRefundAmount(BigDecimal.TEN);

			bean.setRefundProductNum(1);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证code");
			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals("refundBillNo:退款单号不能为空", call.getString("msg"),
					"验证msg");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
